﻿using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Resources;
using System.DirectoryServices;



/// <summary>
/// Summary description for BaseFunction
/// </summary>

namespace Utility
{
    public class BaseFunctions
    {
        #region Fields


        public static string TRUE = "01";
        public static string FALSE = "00";
        public static string COMBOBOX_NULL_VALUE = "- - -";




        #region Default Mail Template
        public const string DEFAULT_REQUEST_TEMPLATE_MAIL = "~/TemplateMails/E04-RequestAccount.txt";

        #endregion Default Mail Template
        public static string DEFAULT_MONTH_NAME = "Janury|Febuary|December";
        public static string DEFAULT_DATE_FORMAT = "dd/MM/yyyy";

        public static string ENCODE_CHAR = "LbGgDesS!@";


        #region Message Error

        public const string E011 = "E011";
        public const string E009 = "E009";
        public const string E007 = "E007";
        public const string E012 = "E012";
        public const string E013 = "E013";
        #endregion

        #region Define CodeMst
        public const string MENU = "001";
        #endregion Define CodeMst

        #region Define Format Valid
        public const string EXP_EMAIL_VALID = "^([a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
        public const string EXP_PHONE_VALID = "^(\\d|,)*\\.?\\d*$";
        #endregion Define Format Valid

        #endregion Fields

        #region Contructors
        public BaseFunctions()
        {
        }
        #endregion Contructors

        #region Methods

        #region Send Mail
        /// <summary>
        /// Send mail	without authentication , text format
        /// </summary>
        /// <param name="_from"></param>
        /// <param name="_to"></param>
        /// <param name="_subject"></param>
        /// <param name="_body"></param>
        /// <param name="_serverName"></param>
        /// <param name="_emailServerPort"></param>
        /// <returns></returns>
        public static bool SendMail(string _from, string _to, string _subject, string _body, string _serverName, int _emailServerPort)
        {
            Utility.MailMessage mail = new Utility.MailMessage();
            mail.From = _from;
            mail.Subject = _subject;
            mail.To = _to;
            mail.Body = _body;
            mail.ServerName = _serverName;
            mail.EmailServerPort = _emailServerPort;
            return mail.SendMail();
        }
        /// <summary>
        /// Send mail	without authentication , html format
        /// </summary>
        /// <param name="_from"></param>
        /// <param name="_to"></param>
        /// <param name="_subject"></param>
        /// <param name="_body"></param>
        /// <param name="_serverName"></param>
        /// <param name="_emailServerPort"></param>
        /// <returns></returns>
        public static bool SendMail(string _from, string _to, string _subject, string _body, string _serverName, int _emailServerPort, bool _isHtml)
        {
            Utility.MailMessage mail = new Utility.MailMessage();
            mail.From = _from;
            mail.Subject = _subject;
            mail.To = _to;
            mail.Body = _body;
            mail.ServerName = _serverName;
            mail.EmailServerPort = _emailServerPort;
            mail.IsHtml = _isHtml;
            return mail.SendMail();
        }
        /// <summary>
        /// Send mail text format
        /// </summary>
        /// <param name="_userName">Ten dang nhap</param>
        /// <param name="_passWord">Mat khau</param>
        /// <param name="_from">Nguoi gui</param>
        /// <param name="_to">Nguoi nhan</param>
        /// <param name="_bcc">Dinh kem</param>
        /// <param name="_subject">Chu de</param>
        /// <param name="_body">Noi dung can gui</param>
        /// <param name="_serverName">Ten server</param>
        /// <returns></returns>
        public static bool SendMail(string _userName, string _passWord, string _from, string _to, string _bcc, string _subject, string _body, string _serverName, int _emailServerPort, bool _ssl)
        {
            Utility.MailMessage mail = new Utility.MailMessage();
            mail.From = _from;
            mail.Subject = _subject;
            mail.To = _to;
            mail.Body = _body;
            mail.Bcc = _bcc;
            // mail.LogFile = Global.fileLogPath;
            mail.ServerName = _serverName;
            mail.EmailServerPort = _emailServerPort;
            mail.SSL = _ssl;
            mail.Username = _userName;
            mail.Password = _passWord;
            return mail.SendMail();
        }

        /// <summary>
        /// Send mail Html format
        /// </summary>
        /// <param name="_userName">Ten dang nhap</param>
        /// <param name="_passWord">Mat khau</param>
        /// <param name="_from">Nguoi gui</param>
        /// <param name="_to">Nguoi nhan</param>
        /// <param name="_bcc">Dinh kem</param>
        /// <param name="_subject">Chu de</param>
        /// <param name="_body">Noi dung can gui</param>
        /// <param name="_serverName">Ten server</param>
        /// <param name="_ssl">ssl</param>
        /// <param name="_isHtml">html format </param>
        /// <returns></returns>
        public static bool SendMail(string _userName, string _passWord, string _from, string _to, string _bcc, string _subject, string _body, string _serverName, int _emailServerPort, bool _ssl, bool _isHtml)
        {
            Utility.MailMessage mail = new Utility.MailMessage();
            mail.From = _from;
            mail.Subject = _subject;
            mail.To = _to;
            mail.Body = _body;
            mail.Bcc = _bcc;
            //mail.LogFile = Global.fileLogPath;
            mail.ServerName = _serverName;
            mail.EmailServerPort = _emailServerPort;
            mail.SSL = _ssl;
            mail.Username = _userName;
            mail.Password = _passWord;
            mail.IsHtml = _isHtml;
            return mail.SendMail();
        }
        #endregion Send Mail

        #region SetValue to Controls
       
       
        #endregion SetValue to Controls

        #region Encode Decode
        /// <summary>
        /// Encode a string value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <summary>
        /// Main key (32 bytes)
        /// </summary>
        public static string ToEncoding(string value)
        {
            return Utility.Encryption64.Encrypt(value, BaseFunctions.ENCODE_CHAR).ToLower();
        }
        /// <summary>
        /// Encode string with unicode
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToEncodingUnicode(string value)
        {
            return Utility.Encryption64.EncryptUnicode(value, BaseFunctions.ENCODE_CHAR).ToLower();
        }
        /// <summary>
        /// Decode a ecoded string value
        /// </summary>
        /// <param name="encodedvalue"></param>
        /// <returns></returns>
        public static string ToDecoding(string encodedvalue)
        {
            return Utility.Encryption64.Decrypt(encodedvalue, BaseFunctions.ENCODE_CHAR);
        }
        /// <summary>
        /// Decode code a string that encoded with unicode
        /// </summary>
        /// <param name="encodedvalue"></param>
        /// <returns></returns>
        public static string ToDecodingUnicode(string encodedvalue)
        {
            return Utility.Encryption64.DecryptUnicode(encodedvalue, BaseFunctions.ENCODE_CHAR);
        }
        #endregion Encode Decode

       
        #region For Methods File
        /// <summary>
        /// Read content of error message by messageID from a file.
        /// </summary>
        /// <param name="_path">File contain error message</param>
        /// <param name="_messageId">Message id</param>
        /// <returns></returns>
        public static string GetMessage(string _path, string _messageId)
        {
            if (File.Exists(_path))
            {
                StreamReader srReadLine = new StreamReader(_path, System.Text.Encoding.GetEncoding("UTF-8"));
                srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);

                while (true)
                {
                    string str = srReadLine.ReadLine();
                    if (str == null)
                        break;
                    try
                    {
                        string[] temp = str.Split('=');
                        string id = temp[0].Replace("var", "").Trim();
                        if (id == _messageId)
                        {
                            string message = temp[1].Replace("\"", "").Replace("'", "").Replace(";", "").Trim();
                            srReadLine.Close();
                            return message;
                        }
                    }
                    catch
                    {
                    }
                }
                srReadLine.Close();
            }
            return string.Empty;
        }

        #endregion For Methods File

        #region Format value

        /// <summary>
        /// Used to format string date to	 CurrentCulture.		
        /// </summary>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static string FormatDateTime(string _value)
        {
            if (_value == null || _value == string.Empty)
                return string.Empty;
            try
            {
                DateTime date = DateTime.Parse(_value);
                CultureInfo cult = Thread.CurrentThread.CurrentCulture;
                return date.ToString("d", cult);
            }
            catch
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// Format data following Language
        /// return empty when _value is nulll or minvalue
        /// </summary>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static string ParseString(DateTime _value)
        {
            if (_value == null || _value == DateTime.MinValue)
                return string.Empty;
            CultureInfo cult = Thread.CurrentThread.CurrentCulture;
            return _value.ToString("d", cult);
        }

        /// <summary>
        /// Add commas separator to _value,
        /// round to nf floating point number
        /// </summary>
        /// <param name="_value">Formating number</param>
        /// <returns>Formated number string</returns>
        public static string ParseString(long _value)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            CultureInfo cult = Thread.CurrentThread.CurrentCulture;
            //cult.NumberFormat.NumberDecimalDigits = 2;
            NumberFormatInfo nfi = cult.NumberFormat;
            return _value.ToString("N", nfi) + " " + nfi.CurrencySymbol;
        }

        /// <summary>
        /// Add commas separator to _value,
        /// round to nf floating point number
        /// </summary>
        /// <param name="_value">Formating number</param>
        /// <returns>Formated number string</returns>
        public static string NumericFormat(int _value)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberDecimalDigits = 0;
            return _value.ToString("N", nfi);
        }

        /// <summary>
        /// Add commas separator to _value,
        /// round to nf floating point number
        /// </summary>
        /// <param name="_value">Formating number</param>
        /// <returns>Formated number string</returns>
        public static string NumericFormat(double _value)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberDecimalDigits = 1;
            nfi.NumberGroupSeparator = ".";
            nfi.NumberDecimalSeparator = ",";
            return _value.ToString("N", nfi);
        }

        /// <summary>
        /// Add commas separator to _value,
        /// round to nf floating point number
        /// </summary>
        /// <param name="_value">Formating number</param>
        /// <returns>Formated number string</returns>
        public static string NumericFormat(decimal _value)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberDecimalDigits = 0;
            nfi.NumberGroupSeparator = ".";
            nfi.NumberDecimalSeparator = ",";
            return _value.ToString("N", nfi);
        }

        /// <summary>
        /// Add commas separator to _value,
        /// round to nf floating point number
        /// </summary>
        /// <param name="_value">Formating number</param>
        /// <returns>Formated number string</returns>
        public static string NumericFormat(decimal _value, int _digit)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberDecimalDigits = _digit;
            nfi.NumberGroupSeparator = ".";
            nfi.NumberDecimalSeparator = ",";
            return _value.ToString("N", nfi);
        }

        /// <summary>
        /// Add commas separator to _value,
        /// round to nf floating point number
        /// </summary>
        /// <param name="_value">Formating number</param>
        /// <param name="_nf">Number of floating point number</param>
        /// <returns>Formated number string</returns>
        public static string NumericFormat(double _value, int _nf)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberDecimalDigits = _nf;
            return _value.ToString("N", nfi);
        }

        /// <summary>
        /// Remove value zero at ended of Decimal number when show UI
        /// </summary>
        /// <param name="_paDecimal">Paramater is Decimal number </param>
        /// <returns>String </returns>
        public static string RemoveDigit0EndOfDecimal(decimal _paDecimal)
        {
            string strValue = _paDecimal.ToString();
            string strVal = string.Empty;
            if (strValue.Length > 0)
            {
                string[] tmpStr = strValue.Split(',');
                if (Convert.ToInt16(tmpStr[1]) == 0)
                {
                    strVal = tmpStr[0].ToString();
                }
                else
                {
                    strVal = strValue;
                }
            }
            return strVal;
        }

        #endregion Format value

        #region Modify Control
        /// <summary>
        /// Set defautl button
        /// </summary>
        public static void SetProcessButton(Page page, Button btnSubmit)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
            sb.Append("var oldPage_IsValid = Page_IsValid; var oldPage_BlockSubmit = Page_BlockSubmit;");
            sb.Append("if (Page_ClientValidate('" + btnSubmit.ValidationGroup + "') == false) {");
            sb.Append(" Page_IsValid = oldPage_IsValid; Page_BlockSubmit = oldPage_BlockSubmit; return false; }} ");
            sb.Append("this.disabled = true;");
            sb.Append(page.ClientScript.GetPostBackEventReference(btnSubmit, null) + ";");
            sb.Append("return true;");
            string submit_button_onclick_js = sb.ToString();
            btnSubmit.Attributes.Add("onclick", submit_button_onclick_js);
        }


        /// <summary>
        /// Remove html tags out of string html
        /// Rerurn text string
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string ConvertHtmlToText(string html)
        {
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("<[^>]*>");
            return reg.Replace(html, "");
        }

        /// <summary>
        /// Get 1 or 2 sentense ( about num words )from large paragraph.
        /// </summary>
        /// <param name="vrParaph">Chuoi can tach</param>
        /// <param name="vrCount">So tu can tach</param>
        /// <returns>So tu can lay + ...</returns>
        public static string GetSentense(string vrParaph, int vrCount)
        {
            char[] strSplit = { ' ', '\r' };
            string[] arrStr = vrParaph.Split(strSplit);

            if (arrStr.Length > vrCount)
            {
                vrParaph = "";
                int i = 0;
                foreach (string vrStr in arrStr)
                {
                    vrParaph = vrParaph + vrStr + " ";
                    i++;
                    if (i > vrCount - 1)
                    {
                        vrParaph = vrParaph + " ...";
                        break;
                    }
                }
                vrParaph = vrParaph.Trim().Replace("\n", "<br>");
                return vrParaph;
            }
            else
            {
                return vrParaph;
            }
        }

        /// <summary>
        /// Get 1 or 2 sentense ( about num words )from large paragraph. Used to show popup when mouse over item or row.
        /// </summary>
        /// <param name="vrParaph">Chuoi can tach</param>
        /// <param name="vrCount">So tu can tach</param>
        /// <param name="isShowPopup">Nếu có ... thì kết quả là true và hiển thị popup lên</param>
        /// <returns>So tu can lay + ...</returns>
        public static string GetSentense(string vrParaph, int vrCount, out bool isShowPopup)
        {
            char[] strSplit = { ' ', '\n' };
            string[] arrStr = vrParaph.Split(strSplit);

            if (arrStr.Length > vrCount)
            {
                vrParaph = "";
                int i = 0;
                foreach (string vrStr in arrStr)
                {
                    vrParaph = vrParaph + vrStr + " ";
                    i++;
                    if (i > vrCount - 1)
                    {
                        vrParaph = vrParaph + " ...";
                        break;
                    }
                }
                isShowPopup = true;
                vrParaph = vrParaph.Trim().Replace("\n", "<br>");
                return vrParaph;
            }
            else
            {
                isShowPopup = false;
                return vrParaph;
            }
        }

        /// <summary>
        /// Get title label. Used to get title of item to show error message.
        /// </summary>
        /// <param name="_lbl">Label control</param>
        /// <returns>a string without (*):</returns>
        public static string GetTitleLabel(Label _lbl)
        {
            if (_lbl.Text.Trim().Length <= 0)
                return string.Empty;
            string result;
            string title = _lbl.Text.Trim();
            if (title.EndsWith("(*):"))
            {
                result = title.Substring(0, title.Length - 4);
            }
            else if (title.EndsWith(":"))
            {
                result = title.Substring(0, title.Length - 1);
            }
            else
            {
                result = title;
            }
            return result;
        }
        /// <summary>
        /// Get title label from login view. Used to get title of item to show error message.
        /// Remove char : or char (*):
        /// </summary>
        /// <param name="_lbl">Label control</param>
        /// <returns>a string without (*):</returns>
        public static string GetTitleLabelFromLoginView(string _lbl)
        {
            if (_lbl.Trim().Length <= 0)
                return string.Empty;
            string result;
            string title = _lbl.Trim();
            if (title.EndsWith("(*):"))
            {
                result = title.Substring(0, title.Length - 4);
            }
            else if (title.EndsWith(":"))
            {
                result = title.Substring(0, title.Length - 1);
            }
            else
            {
                result = title;
            }
            return result;
        }
        public static string GetTextResource(Label _lb)
        {
            string result;
            try
            {
                result = _lb.Text.Substring(0, _lb.Text.Length - 1);
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }
        /// <summary>
        /// Set css to control textbox status to stage preview( readonly, flat)
        /// </summary>
        /// <param name="txt">TextBox control</param>
        /// <param name="cssclass">Class css</param>
        public static void SetPreviewTextBox(TextBox txt, string cssclass)
        {
            txt.BorderColor = System.Drawing.Color.Transparent;
            txt.CssClass = cssclass;
            txt.ReadOnly = true;
        }
        /// <summary>
        /// Set css to control textbox status to stage editable.
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="cssclass"></param>
        public static void SetNormalTextBox(TextBox txt, string cssclass)
        {
            txt.CssClass = cssclass;
            txt.ReadOnly = false;
        }
        /// <summary>
        /// Disable dropdownlist.
        /// </summary>
        /// <param name="drp"></param>
        /// <param name="cssclass"></param>
        public static void SetPreviewDropDown(DropDownList drp, string cssclass)
        {
            drp.CssClass = cssclass;
            drp.Enabled = false;
        }
        /// <summary>
        /// Endable dropdownlist
        /// </summary>
        /// <param name="drp"></param>
        /// <param name="cssclass"></param>
        public static void SetNormalDropDown(DropDownList drp, string cssclass)
        {
            drp.CssClass = cssclass;
            drp.Enabled = true;
        }

        public static void SetPreviewTextArea(TextBox txt)
        {
            txt.ReadOnly = true;
            txt.Attributes.Add("style", "border: 0px;background-color: #f5f5f5;color:black;");
        }

        public static void SetNormalTextArea(TextBox txt)
        {
            txt.Attributes.Remove("style");
            txt.ReadOnly = false;
        }

        /// <summary>
        /// Set default button fireup when user press enter button
        /// </summary>
        /// <param name="page"></param>
        /// <param name="clientID"></param>
        public static void SetDefaultButton(Page page, string clientID)
        {
            page.RegisterStartupScript("DefaultButton", "<script>\r\n\t" +
            "	window.document.onkeydown = function(evt)\r\n\t	{ \r\n\t\t" +
            "var et = (window.event)?event:evt;  \r\n\t\t" +
        "var target;	\r\n\t\t" +
        "if (\"activeElement\" in document){\r\n\t\t" +
        "\r\n\t\t// Internet Explorer and Opera\r\n\t\t" +
        "target = document.activeElement; \r\n\t\t" +
        "}\r\n\t\t" +
        "else {\r\n\t\t" +
        "		// Firefox\r\n\t\t" +
        "\r\n\ttarget = et ? et.explicitOriginalTarget : null;" +
        "}\r\n\t\t" +
        "var defaultButton = frm." + clientID + ";\r\n\t\t" +
        "if (et.keyCode == 13 && !(target && (target.tagName.toLowerCase() == \"textarea\"))&& target.tagName.toLowerCase() != 'a' && target.type != 'image' ) \r\n\t\t" +
        "{ \r\n\t\t " +
         " if( checkbrowser() == 'isIE'){ \r\n\t\t" +
             " if(document.readyState != 'complete'){\r\n\t\t" +
                 " return false;\r\n\t\t" +
                "}\r\n\t\t" +
                "et.returnValue = false;\r\n\t\t" +
                "et.cancel = true;  \r\n\t\t  " +
            "}\r\n\t\t" +
        "if(target.type =='submit'){ \r\n\t\t " +
            "if(target.id == defaultButton.id ){     \r\n\t\t    " +
                "if( Page_ClientValidate()) {\r\n\t\t" +
                    "if( checkbrowser() == 'isIE'){\r\n\t\t " +
                        "defaultButton.click();\r\n\t\t " +
                    "}\r\n\t\t " +
                    "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                        "var e = document.createEvent(\"MouseEvents\");\r\n\t\t" +
                        "e.initEvent(\"click\", true, true);\r\n\t\t" +
                 "   defaultButton.dispatchEvent;\r\n\t\t" +
                "  }\r\n\t\t" +
             " }\r\n\t\t" +
            "}\r\n\t\t" +
            "else {        \r\n\t\t " +
            "  if(checkbrowser() == 'isIE')						\r\n\t\t " +
             " {																		\r\n\t\t " +
            "   target.click();								\r\n\t\t " +
            "  }																 \r\n\t\t" +
            "  else if( checkbrowser() == 'isFF')\r\n\t\t" +
            "  {																						\r\n\t\t			" +
            "    var e = document.createEvent(\"MouseEvents\");	\r\n\t\t	" +
            "    e.initEvent(\"click\", true, true);  \r\n\t\t          " +
            "    target.dispatchEvent;\r\n\t\t" +
            "  }\r\n\t\t" +
         " }\r\n\t\t" +
        "}\r\n\t\t" +
     " else    {\r\n\t\t" +
            "if(Page_ClientValidate())      {\r\n\t\t" +
                "if(checkbrowser() == 'isIE')\r\n\t\t" +
                "{\r\n\t\t" +
                 " defaultButton.click();\r\n\t\t" +
                "}\r\n\t\t" +
                "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                 " var e = document.createEvent(\"MouseEvents\");		\r\n\t\t" +
                 " e.initEvent(\"click\", true, true);            \r\n\t\t" +
                 " defaultButton.dispatchEvent;\r\n\t\t" +
                "} \r\n\t\t" +
            "}\r\n\t\t" +
        "}\r\n\t\t" +
    "}\r\n\t\t" +
"};</script>");
        }

        /// <summary>
        /// Set default button fireup when user press enter button
        /// </summary>
        /// <param name="page"></param>
        /// <param name="clientID"></param>
        /// <param name="frm"></param>
        public static void SetDefaultButton(Page page, string clientID, string frm)
        {
            page.RegisterStartupScript("DefaultButton", "<script>\r\n\t" +
            "	window.document.onkeydown = function(evt)\r\n\t	{ \r\n\t\t" +
            "var et = (window.event)?event:evt;  \r\n\t\t" +
        "var target;	\r\n\t\t" +
        "if (\"activeElement\" in document){\r\n\t\t" +
        "\r\n\t\t// Internet Explorer and Opera\r\n\t\t" +
        "target = document.activeElement; \r\n\t\t" +
        "}\r\n\t\t" +
        "else {\r\n\t\t" +
        "		// Firefox\r\n\t\t" +
        "\r\n\ttarget = et ? et.explicitOriginalTarget : null;" +
        "}\r\n\t\t" +
        "var defaultButton = " + frm + "." + clientID + ";\r\n\t\t" +
        "if (et.keyCode == 13 && !(target && (target.tagName.toLowerCase() == \"textarea\"))&& target.tagName.toLowerCase() != 'a' && target.type != 'image' ) \r\n\t\t" +
        "{ \r\n\t\t " +
         " if( checkbrowser() == 'isIE'){ \r\n\t\t" +
             " if(document.readyState != 'complete'){\r\n\t\t" +
                 " return false;\r\n\t\t" +
                "}\r\n\t\t" +
                "et.returnValue = false;\r\n\t\t" +
                "et.cancel = true;  \r\n\t\t  " +
            "}\r\n\t\t" +
        "if(target.type =='submit'){ \r\n\t\t " +
            "if(target.id == defaultButton.id ){     \r\n\t\t    " +
        "if( typeof(Page_ClientValidate) == 'function')  {\r\n\t\t" +
                    "if( checkbrowser() == 'isIE'){\r\n\t\t " +
                        "defaultButton.click();\r\n\t\t " +
                    "}\r\n\t\t " +
                    "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                        "var e = document.createEvent(\"MouseEvents\");\r\n\t\t" +
                        "e.initEvent(\"click\", true, true);\r\n\t\t" +
                 "   defaultButton.dispatchEvent;\r\n\t\t" +
                "  }\r\n\t\t" +
             " }\r\n\t\t" +
            "}\r\n\t\t" +
            "else {        \r\n\t\t " +
            "  if(checkbrowser() == 'isIE')						\r\n\t\t " +
             " {																		\r\n\t\t " +
            "   target.click();								\r\n\t\t " +
            "  }																 \r\n\t\t" +
            "  else if( checkbrowser() == 'isFF')\r\n\t\t" +
            "  {																						\r\n\t\t			" +
            "    var e = document.createEvent(\"MouseEvents\");	\r\n\t\t	" +
            "    e.initEvent(\"click\", true, true);  \r\n\t\t          " +
            "    target.dispatchEvent;\r\n\t\t" +
            "  }\r\n\t\t" +
         " }\r\n\t\t" +
        "}\r\n\t\t" +
     " else    {\r\n\t\t" +
      "if (typeof(Page_ClientValidate) == 'function')       {\r\n\t\t" +
                "if(checkbrowser() == 'isIE')\r\n\t\t" +
                "{\r\n\t\t" +
                 " defaultButton.click();\r\n\t\t" +
                "}\r\n\t\t" +
                "else if( checkbrowser() == 'isFF'){ \r\n\t\t" +
                 " var e = document.createEvent(\"MouseEvents\");		\r\n\t\t" +
                 " e.initEvent(\"click\", true, true);            \r\n\t\t" +
                 " defaultButton.dispatchEvent;\r\n\t\t" +
                "} \r\n\t\t" +
            "}\r\n\t\t" +
        "}\r\n\t\t" +
    "}\r\n\t\t" +
"};</script>");
        }

        /// <summary>
        /// Adds the onfocus and onblur attributes to all input controls found in the specified parent,
        /// to change their apperance with the control has the focus
        /// </summary>
        public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes)
        {
            foreach (Control ctl in container.Controls)
            {
                if ((onlyTextBoxes && ctl is TextBox) || ctl is TextBox || ctl is DropDownList ||
                    ctl is ListBox || ctl is CheckBox || ctl is RadioButton ||
                    ctl is RadioButtonList || ctl is CheckBoxList)
                {
                    WebControl wctl = ctl as WebControl;
                    wctl.Attributes.Add("onfocus", string.Format("this.className = '{0}';", className));
                    wctl.Attributes.Add("onblur", "this.className = '';");
                }
                else
                {
                    if (ctl.Controls != null && ctl.Controls.Count > 0)
                        SetInputControlsHighlight(ctl, className, onlyTextBoxes);
                }
            }
        }


        public static string GetTextLabelControl(Label lbl)
        {
            string result = lbl.Text.Replace(":", "");
            return result;
        }
        public static string GetTextLabelControl(Literal lbl)
        {
            string result = lbl.Text.Replace(":", "");
            return result;
        }
       
        #endregion Methods

        #endregion
    }
}



